package pl.edu.pw.MidEmu.Logic;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Hashtable;
import java.util.Properties;

import pl.edu.pw.MidEmu.Common.Consts;

/**
 * Service class responsible for configuring MIDAS and CBRD instances 
 * @author Mateusz Starodab
 *
 */
public class ConfigurationService
{
	/**
	 * Configures CBRD demon
	 * @param absolutePath Path to configuration files 
	 * @param nodeInterfaceName Interface name
	 * @param interfacesName List of all interfaces used by MidEmu
	 * @param centralNodeCBRDId Central node id
	 * @param centralNodeIP Central node IP
	 * @param nodeIds List of all node IDs
	 * @param nodeIP NodeIp
	 */
	public void setCBRDDemonConfig(String absolutePath, String nodeInterfaceName, Hashtable<String, String> interfacesName, String centralNodeCBRDId, String centralNodeIP, Hashtable<String, String> nodeIds, String nodeIP)
	{
		String mappings = nodeIds.size() + "\n";

		for (String key : nodeIds.keySet())
		{
			mappings += key + " " + nodeIds.get(key) + "\n";
		}

		try
		{
			Writer networkInterfacesOutput = new BufferedWriter(new FileWriter(absolutePath + java.io.File.separatorChar + Consts.CBRD_MappingsConfigFile));
			networkInterfacesOutput.write(mappings);
			networkInterfacesOutput.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}

		String networkInterfaces = interfacesName.size() + "\n";
		
		for(String key : interfacesName.keySet())
		{
			networkInterfaces += key + " " + interfacesName.get(key) + "\n";
		}
		
		//String networkInterfaces = "1\n" + nodeInterfaceName + " " + nodeIP;// + " adhoc"; 

		try
		{
			Writer networkInterfacesOutput = new BufferedWriter(new FileWriter(absolutePath + java.io.File.separatorChar + Consts.CBRD_NetworkConfigFile));
			networkInterfacesOutput.write(networkInterfaces);
			networkInterfacesOutput.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}

		String centralNode = centralNodeCBRDId + "\n" + centralNodeIP;
		try
		{
			Writer centralNodeOutput = new BufferedWriter(new FileWriter(absolutePath + java.io.File.separatorChar + Consts.CBRD_CentralNodeConfigFile));
			centralNodeOutput.write(centralNode);
			centralNodeOutput.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	/**
	 * Configures MIDAS platform central node
	 * @param workDir Work directory path
	 * @param nodeID Node ID
	 * @param selfIP Self IP address
	 */
	public void setCentralNodeConfiguration(String workDir, String nodeID, String selfIP)
	{
		FileInputStream CRTConfFileInStream = null;
		FileInputStream MWConfFileInStream = null;
		FileOutputStream CRTConfFileOutStream = null;
		FileOutputStream MWConfFileOutStream = null;
		try
		{
			Properties propsCRT = new Properties();
			CRTConfFileInStream = new FileInputStream(workDir + java.io.File.separatorChar + Consts.CRTConfiguration_FileName);
			propsCRT.load(CRTConfFileInStream);
			CRTConfFileInStream.close();

			propsCRT.setProperty(Consts.CRTConfiguration_nodeId, nodeID);
			propsCRT.setProperty(Consts.CRTConfiguration_NodeType, "0");
			propsCRT.setProperty(Consts.CRTConfiguration_CBRServerIP, selfIP);
			propsCRT.setProperty(Consts.CRTConfiguration_CBRNodeID, nodeID);
			propsCRT.setProperty(Consts.CRTConfiguration_PublicIPAddress, selfIP);
			propsCRT.setProperty(Consts.CRTConfiguration_CentralNodeIP, selfIP);

			CRTConfFileOutStream = new FileOutputStream(workDir + java.io.File.separatorChar + Consts.CRTConfiguration_FileName);
			propsCRT.store(CRTConfFileOutStream, null);
			CRTConfFileOutStream.close();

			Writer centralNodeConfFileOutStream = new BufferedWriter(new FileWriter(workDir + java.io.File.separatorChar + Consts.CNConfiguration_FileName, false));
			String centralNodeIP = "tcp://" + selfIP + ":9701";
			centralNodeConfFileOutStream.write(centralNodeIP);
			centralNodeConfFileOutStream.close();

			Properties propsMW = new Properties();
			MWConfFileInStream = new FileInputStream(workDir + java.io.File.separatorChar + Consts.MWConfiguration_FileName);
			propsMW.load(MWConfFileInStream);
			MWConfFileInStream.close();

			propsMW.setProperty(Consts.MWConfiguration_ServerSocketIpAddress, selfIP);

			MWConfFileOutStream = new FileOutputStream(workDir + java.io.File.separatorChar + Consts.MWConfiguration_FileName);
			propsMW.store(MWConfFileOutStream, null);
			MWConfFileOutStream.close();

			Writer ifOrderFileOutStream = new BufferedWriter(new FileWriter(workDir + java.io.File.separatorChar + Consts.IfOrderConfiguration_FileName, false));
			String ifOrderOrderConf = Consts.IfOrderConfiguration_ETH + "=" + nodeID + ":" + selfIP + ":0";
			ifOrderFileOutStream.write(ifOrderOrderConf);
			ifOrderFileOutStream.close();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}

	/**
	 * Configures MIDAS platform user node
	 * @param workDir Work directory path
	 * @param nodeID Node ID
	 * @param selfIP Self IP
	 * @param centralNodeID Central node ID
	 * @param centralNodeIP Central node IP
	 */
	public void setUserNodeConfiguration(String workDir, String nodeID, String selfIP, String centralNodeID, String centralNodeIP)
	{
		FileInputStream CRTConfFileInStream = null;
		FileInputStream MWConfFileInStream = null;
		FileOutputStream CRTConfFileOutStream = null;
		FileOutputStream MWConfFileOutStream = null;
		try
		{
			Properties propsCRT = new Properties();
			CRTConfFileInStream = new FileInputStream(workDir + java.io.File.separatorChar + Consts.CRTConfiguration_FileName);
			propsCRT.load(CRTConfFileInStream);
			CRTConfFileInStream.close();

			propsCRT.setProperty(Consts.CRTConfiguration_nodeId, nodeID);
			propsCRT.setProperty(Consts.CRTConfiguration_NodeType, "1");
			propsCRT.setProperty(Consts.CRTConfiguration_CBRServerIP, selfIP);
			propsCRT.setProperty(Consts.CRTConfiguration_CBRNodeID, nodeID);
			propsCRT.setProperty(Consts.CRTConfiguration_PublicIPAddress, selfIP);
			propsCRT.setProperty(Consts.CRTConfiguration_CentralNodeIP, centralNodeIP);

			CRTConfFileOutStream = new FileOutputStream(workDir + java.io.File.separatorChar + Consts.CRTConfiguration_FileName);
			propsCRT.store(CRTConfFileOutStream, null);
			CRTConfFileOutStream.close();

			Writer centralNodeConfFileOutStream = new BufferedWriter(new FileWriter(workDir + java.io.File.separatorChar + Consts.CNConfiguration_FileName, false));
			String centralNodeIPString = "tcp://" + centralNodeIP + ":9701";
			centralNodeConfFileOutStream.write(centralNodeIPString);
			centralNodeConfFileOutStream.close();

			Properties propsMW = new Properties();
			MWConfFileInStream = new FileInputStream(workDir + java.io.File.separatorChar + Consts.MWConfiguration_FileName);
			propsMW.load(MWConfFileInStream);
			MWConfFileInStream.close();

			propsMW.setProperty(Consts.MWConfiguration_ServerSocketIpAddress, selfIP);

			MWConfFileOutStream = new FileOutputStream(workDir + java.io.File.separatorChar + Consts.MWConfiguration_FileName);
			propsMW.store(MWConfFileOutStream, null);
			MWConfFileOutStream.close();

			Writer ifOrderFileOutStream = new BufferedWriter(new FileWriter(workDir + java.io.File.separatorChar + Consts.IfOrderConfiguration_FileName, false));
			String ifOrderOrderConf = Consts.IfOrderConfiguration_ETH + "=" + centralNodeID + ":" + centralNodeIP + ":0";
			ifOrderFileOutStream.write(ifOrderOrderConf);
			ifOrderFileOutStream.close();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}

}
